package net.httpconn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

public class GetCommand extends HttpCommand {

    public String execute() throws IOException {

        StringBuffer responseBuffer = new StringBuffer();
        BufferedReader bReader = null;
        String response = null;
        System.out.println("connection: " + connection);

        System.out.println("connection.getResponseCode: " + connection.getResponseCode());
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {

            bReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = null;

            while ((line = bReader.readLine()) != null) {
                responseBuffer.append(line + "\n");
            }
        } else {
            responseBuffer.append(connection.getResponseCode());
        }
        response = responseBuffer.toString();

        bReader.close();


        super.finalizeConnection();


        return response;
    }
}
